Skip to content

[Discover] [ES|QL] fix: stats by previous aggregated field - #290259

Merged
sddonne merged 2 commits into
elastic:mainfrom
sddonne:fix/esql-cascade-group-by-prior-aggregate
Sep 10, 2026
Merged

[Discover] [ES|QL] fix: stats by previous aggregated field#290259
sddonne merged 2 commits into
elastic:mainfrom
sddonne:fix/esql-cascade-group-by-prior-aggregate

Conversation

@sddonne

@sddonne sddonne commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes an unhandled TypeError: undefined is not an object (evaluating 'groupFieldNode.arg') thrown by getESQLStatsQueryMeta (used by Discover's ES|QL "cascade documents" breakdown feature) when a query has two STATS commands and the second groups BY a field that the first STATS declared as an aggregate alias rather than a BY-grouping alias.

Repro

FROM kibana_sample_data_logs
| STATS x = MAX(bytes)
| STATS c = COUNT(*) BY x

Solution

x should be treated as a regular column, the code was incorrectly branching into a special case as x was defined in the previous STATS command (expecting to be found as new group definition).
Also added a catch to avoid buggy UI when analysing the query fails, (discover was held in a permanent loading state before the fix)

Before

image

After

image

Checklist

@sddonne sddonne self-assigned this Sep 10, 2026
@sddonne sddonne added Team:DataDiscovery Discover, search (data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. t// Team:ESQL ES|QL related features in Kibana t// v9.6.0 release_note:fix backport:skip This PR does not require backporting Feature:Discover Discover Application labels Sep 10, 2026
@kibanamachine

Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
shared-packages 4.6MB 4.6MB +261.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
@kbn/esql-utils 3 4 +1

Total ESLint disabled count

id before after diff
@kbn/esql-utils 3 4 +1

total optimizer output size

id before after diff
all 63.8MB 63.8MB +261.0B

warm start memory

id before after diff
post forced gc heap baseline - 833907234 +833907234
post forced gc heap delta - -1664186 -1664186
post forced gc heap delta standard deviation - 766137 +766137
post forced gc heap target - 832243048 +832243048
tail heap delta - -26035082 -26035082
total +1639217151

cc @sddonne

@sddonne
sddonne marked this pull request as ready for review September 10, 2026 11:14
@sddonne
sddonne requested a review from a team as a code owner September 10, 2026 11:14
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

Pinging @elastic/kibana-data-discovery (Team:DataDiscovery)

@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

Pinging @elastic/kibana-esql (Team:ESQL)

@stratoula stratoula left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review only, LGTM!

* This method will exclude queries contain commands that are not valid for the cascade experience,
*/
export const getESQLStatsQueryMeta = (queryString: string): ESQLStatsQueryMeta => {
try {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@sddonne sddonne added v9.5.5 backport:version Backport to applied version labels and removed backport:skip This PR does not require backporting labels Sep 10, 2026
@sddonne
sddonne merged commit d5e1a4e into elastic:main Sep 10, 2026
302 checks passed
@kibanamachine

Copy link
Copy Markdown
Contributor

Starting backport for target branches: 9.5

https://github.com/elastic/kibana/actions/runs/34474214078

@kibanamachine

Copy link
Copy Markdown
Contributor

💚 All backports created successfully

Status Branch Result
9.5

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Sep 10, 2026
…0259) (#290297)

# Backport

This will backport the following commits from `main` to `9.5`:
- [[Discover] [ES|QL] fix: stats by previous aggregated field
(#290259)](#290259)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Sebastian Delle
Donne","email":"sebastian.delledonne@elastic.co"},"sourceCommit":{"committedDate":"2026-09-10T11:59:28Z","message":"[Discover]
[ES|QL] fix: stats by previous aggregated field (#290259)\n\n- Closes
https://github.com/elastic/kibana/issues/290060\n## Summary\n\nFixes an
unhandled `TypeError: undefined is not an object
(evaluating\n'groupFieldNode.arg')` thrown by `getESQLStatsQueryMeta`
(used by\nDiscover's ES|QL \"cascade documents\" breakdown feature) when
a query has\ntwo `STATS` commands and the second groups `BY` a field
that the first\n`STATS` declared as an **aggregate** alias rather than a
`BY`-grouping\nalias.\n\n### Repro\n\n```esql\nFROM
kibana_sample_data_logs\n| STATS x = MAX(bytes)\n| STATS c = COUNT(*) BY
x\n```\n\n### Solution\n`x` should be treated as a regular column, the
code was incorrectly\nbranching into a special case as `x` was defined
in the previous `STATS`\ncommand (expecting to be found as new group
definition).\nAlso added a catch to avoid buggy UI when analysing the
query fails,\n(discover was held in a permanent loading state before the
fix)\n\n#### Before\n<img width=\"1724\" height=\"945\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/17ba47a5-84f3-48fc-b09f-bb6263d1ae07\"\n/>\n\n####
After\n<img width=\"1723\" height=\"902\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/c137f927-01e3-4705-8d0d-ad50663f08e0\"\n/>\n\n\n\n###
Checklist\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common
scenarios","sha":"d5e1a4e37970834570aaa52d0488081ded16123c","branchLabelMapping":{"^v9.6.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Discover","release_note:fix","Team:DataDiscovery","Team:ESQL","backport:version","v9.6.0","v9.5.5"],"title":"[Discover]
[ES|QL] fix: stats by previous aggregated
field","number":290259,"url":"https://github.com/elastic/kibana/pull/290259","mergeCommit":{"message":"[Discover]
[ES|QL] fix: stats by previous aggregated field (#290259)\n\n- Closes
https://github.com/elastic/kibana/issues/290060\n## Summary\n\nFixes an
unhandled `TypeError: undefined is not an object
(evaluating\n'groupFieldNode.arg')` thrown by `getESQLStatsQueryMeta`
(used by\nDiscover's ES|QL \"cascade documents\" breakdown feature) when
a query has\ntwo `STATS` commands and the second groups `BY` a field
that the first\n`STATS` declared as an **aggregate** alias rather than a
`BY`-grouping\nalias.\n\n### Repro\n\n```esql\nFROM
kibana_sample_data_logs\n| STATS x = MAX(bytes)\n| STATS c = COUNT(*) BY
x\n```\n\n### Solution\n`x` should be treated as a regular column, the
code was incorrectly\nbranching into a special case as `x` was defined
in the previous `STATS`\ncommand (expecting to be found as new group
definition).\nAlso added a catch to avoid buggy UI when analysing the
query fails,\n(discover was held in a permanent loading state before the
fix)\n\n#### Before\n<img width=\"1724\" height=\"945\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/17ba47a5-84f3-48fc-b09f-bb6263d1ae07\"\n/>\n\n####
After\n<img width=\"1723\" height=\"902\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/c137f927-01e3-4705-8d0d-ad50663f08e0\"\n/>\n\n\n\n###
Checklist\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common
scenarios","sha":"d5e1a4e37970834570aaa52d0488081ded16123c"}},"sourceBranch":"main","suggestedTargetBranches":["9.5"],"targetPullRequestStates":[{"branch":"main","label":"v9.6.0","branchLabelMappingKey":"^v9.6.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/290259","number":290259,"mergeCommit":{"message":"[Discover]
[ES|QL] fix: stats by previous aggregated field (#290259)\n\n- Closes
https://github.com/elastic/kibana/issues/290060\n## Summary\n\nFixes an
unhandled `TypeError: undefined is not an object
(evaluating\n'groupFieldNode.arg')` thrown by `getESQLStatsQueryMeta`
(used by\nDiscover's ES|QL \"cascade documents\" breakdown feature) when
a query has\ntwo `STATS` commands and the second groups `BY` a field
that the first\n`STATS` declared as an **aggregate** alias rather than a
`BY`-grouping\nalias.\n\n### Repro\n\n```esql\nFROM
kibana_sample_data_logs\n| STATS x = MAX(bytes)\n| STATS c = COUNT(*) BY
x\n```\n\n### Solution\n`x` should be treated as a regular column, the
code was incorrectly\nbranching into a special case as `x` was defined
in the previous `STATS`\ncommand (expecting to be found as new group
definition).\nAlso added a catch to avoid buggy UI when analysing the
query fails,\n(discover was held in a permanent loading state before the
fix)\n\n#### Before\n<img width=\"1724\" height=\"945\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/17ba47a5-84f3-48fc-b09f-bb6263d1ae07\"\n/>\n\n####
After\n<img width=\"1723\" height=\"902\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/c137f927-01e3-4705-8d0d-ad50663f08e0\"\n/>\n\n\n\n###
Checklist\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common
scenarios","sha":"d5e1a4e37970834570aaa52d0488081ded16123c"}},{"branch":"9.5","label":"v9.5.5","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Sebastian Delle Donne <sebastian.delledonne@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:version Backport to applied version labels Feature:Discover Discover Application release_note:fix Team:DataDiscovery Discover, search (data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. t// Team:ESQL ES|QL related features in Kibana t// v9.5.4 v9.5.5 v9.6.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Discover] ES|QL cascade documents: Error when STATS ... BY groups by a field created via aggregate in a preceding STATS

3 participants